package com.zlz.mapreduce.partition;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @author: zlz
 * @date: 2022/1/1 21:34
 * @description:
 * @since: JDK1.8
 */
public class FlowMapper extends Mapper<LongWritable, Text, Text, FlowBean> {
    private final Text outKey = new Text();
    private final FlowBean outValue = new FlowBean();
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 获取一行的数据
        String line = value.toString();

        // 格式化 将\t替换为空格
        String replace = line.replace("\t", StringUtils.SPACE);

        // 切割
        String[] split = StringUtils.split(replace, StringUtils.SPACE);

        // 获取数据
        String phone = split[1];
        String up = split[split.length - 3];
        String down = split[split.length - 2];

        // 封装
        outKey.set(phone);
        outValue.setUpFlow(Long.parseLong(up));
        outValue.setDownFlow(Long.parseLong(down));
        outValue.setTotalFlow();

        // 写出
        context.write(outKey, outValue);

    }
}
